#region License
// PostController.cs
// 
// Copyright (c) 2012 Xoqal.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Kaqaz.Web.UI.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Configuration;
    using System.Web.Mvc;
    using Xoqal.Data;
    using Xoqal.Web.Mvc.Models;
    using Xoqal.Web.Mvc.Security;

    public class PostController : DataDrivenController
    {
        private readonly Data.IPostRepository postRepository;
        private readonly Services.PostService postService;

        /// <summary>
        /// Initializes a new instance of the <see cref="PostController" /> class.
        /// </summary>
        /// <param name="unitOfWork">The unit of work.</param>
        /// <param name="postRepository">The post repository.</param>
        /// <param name="postService">The post service.</param>
        public PostController(
            IUnitOfWork unitOfWork, 
            Data.IPostRepository postRepository,
            Services.PostService postService)
            : base(unitOfWork)
        {
            this.postRepository = postRepository;
            this.postService = postService;
        }

        /// <summary>
        /// Gets the size of the page.
        /// </summary>
        /// <value>
        /// The size of the page.
        /// </value>
        public int PageSize
        {
            get { return int.Parse(WebConfigurationManager.AppSettings["BlogPostsPageSize"]); }
        }

        /// <summary>
        /// GET: /Blog/Post/1?tag=...
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="tag">The tag.</param>
        /// <returns></returns>
        public ActionResult Index(int page = 1, string tag = null)
        {
            return this.View(this.postService.GetPosts(page, tag, this.PageSize));
        }

        /// <summary>
        /// GET: /Blog/Post/Details/5
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            var postEntity = this.postRepository.GetPost(id);
            if (postEntity == null)
            {
                return new HttpNotFoundResult();
            }

            postEntity.Hits++;

            var postModel = new Models.Post(postEntity);
            postModel.VisibleComments = this.postService.GetVisibleComments(postEntity);

            return this.View(postModel);
        }

        /// <summary>
        /// GET: /Blog/Post/Create
        /// </summary>
        /// <returns></returns>
        [Permission("PostManagement")]
        public ActionResult Create()
        {
            // Return a new object for default values
            return this.View(new Models.Post());
        }

        /// <summary>
        /// POST: /Blog/Post/Create
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [ValidateInput(false)]
        [HttpPost]
        [Permission("PostManagement")]
        public ActionResult Create(Models.Post model)
        {
            try
            {
                var entity = new Entities.Post();
                this.postService.SetPostData(model, entity);
                entity.Hits = 0;

                this.postRepository.Add(entity);

                return this.RedirectToAction("Index");
            }
            catch
            {
                return this.View();
            }
        }

        /// <summary>
        /// GET: /Blog/Post/Edit/5
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        [Permission("PostManagement")]
        public ActionResult Edit(int id)
        {
            var post = this.postRepository.GetPost(id);
            if (post == null)
            {
                return this.RedirectToAction("Index");
            }

            return this.View(new Models.Post(post));
        }

        /// <summary>
        /// POST: /Blog/Post/Edit/5
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [ValidateInput(false)]
        [HttpPost]
        [Permission("PostManagement")]
        public ActionResult Edit(int id, Models.Post model)
        {
            try
            {
                var entity = this.postRepository.GetPost(id);
                if (entity == null)
                {
                    return new HttpNotFoundResult();
                }

                this.postService.SetPostData(model, entity);
                entity.LastUpdateTime = DateTime.Now;

                return this.RedirectToAction("Index");
            }
            catch
            {
                return this.View();
            }
        }

        /// <summary>
        /// GET: /Blog/Post/Delete/5
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        [Permission("PostManagement")]
        public ActionResult Delete(int id)
        {
            var entity = this.postRepository.GetPost(id);
            if (entity == null)
            {
                return new HttpNotFoundResult();
            }

            this.postRepository.Delete(entity);
            return this.View();
        }
    }
}
